package com.market.inventory.server.service.impl;

import com.market.common.core.domain.Result;
import com.market.inventory.server.City;
import com.market.inventory.server.mapper.SamInventoryMapper;
import com.market.inventory.server.purchase.SamPurchase;
import com.market.inventory.server.service.SamInventoryService;
import com.market.inventory.server.vo.VoProductSku;
import com.market.inventory.server.vo.VoPurchase;
import com.market.product.common.domain.ProductInfo;
import com.market.product.common.domain.ProductSkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SamInventoryServiceImpl implements SamInventoryService {
    @Autowired
    SamInventoryMapper samInventoryMapper;

    @Override
    public List<ProductInfo> forewarninglist() {
        List<ProductInfo> list=samInventoryMapper.forewarninglist();
        return list;
    }

    @Override
    public List<ProductSkuInfo> inventoryselect() {
        List<ProductSkuInfo> skuInfos=samInventoryMapper.inventoryselect();
        for (ProductSkuInfo skuInfo : skuInfos) {
            Integer i=samInventoryMapper.forewarninginsert(skuInfo.getProductId());
        }
        return skuInfos;
    }

    @Override
    public Integer purchase(SamPurchase samPurchase) {

        Date date = new Date();
        // 创建SimpleDateFormat对象，并指定日期时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用SimpleDateFormat将Date对象格式化为字符串
        String formattedDate = sdf.format(date);
        samPurchase.setPurchaseTime(formattedDate);
        String skuIds = samPurchase.getProductSkuIds();
        String[] skuIdArray = skuIds.split(",");
        for (String skuId : skuIdArray) {
            ProductSkuInfo productSkuInfo=samInventoryMapper.PurchasePrice(Integer.valueOf(skuId));
            samPurchase.setPurchasePrice(productSkuInfo.getPurchasePrice());
            System.out.println(productSkuInfo.getPurchasePrice());
            Integer i=samInventoryMapper.purchase(Integer.valueOf(skuId),samPurchase);
        }

        return Result.SUCCESS;
    }

    @Override
    public List<ProductSkuInfo> forewarningdetails(Integer id) {
        List<ProductSkuInfo> skuInfos=samInventoryMapper.forewarningdetails(id);
        return skuInfos;
    }

    @Override
    public List<SamPurchase> purchaseList() {
        List<SamPurchase> samPurchases=samInventoryMapper.samPurchases();
        return samPurchases;
    }

    @Override
    public Integer purchaseUpdate(VoProductSku voProductSku) {
        Integer i=samInventoryMapper.statusUpdate(voProductSku.getPurchaseId());
        return samInventoryMapper.purchaseUpdate(voProductSku);
    }

    @Override
    public Result<List<City>> city(Integer cityId) {
        List<City> cityList=samInventoryMapper.city(cityId);
        return Result.success(cityList);
    }


    @Override
    public Integer updateStatus(VoPurchase voPurchase) {
        return samInventoryMapper.updateStatus(voPurchase);
    }


}
